/*
 * JStellaConfigurationDialog.java
 *
 * Created on August 17, 2007, 6:07 PM
 */

package jstella.runner;

import dolmisani.toys.emulators.j2600.J2600Main;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.*;
import jstella.runner.InputMaster.VirtualControlTask;
import jstella.runner.InputMaster.InputControlBinder;
import java.awt.*;
import java.awt.event.*;


import java.io.*;
/**
 *
 * @author  Sysop
 */
public class JStellaConfigurationDialog extends javax.swing.JDialog {
    
    private java.util.Map<String, String> myNewConfiguration=null;
    private ConfigControlTableModel myTableModel=new ConfigControlTableModel();
    private java.util.List<InputMaster.InputControlBinder> myControlItemList=new java.util.ArrayList<InputMaster.InputControlBinder>();
    private InputControlBinder myCurrentControlItem=null;
    
    private int myControlItemKeyCode=0;
    private VirtualControlTask[] myVirtualControlItemArray=VirtualControlTask.values();
    
    /** Creates new form JStellaConfigurationDialog */
    public JStellaConfigurationDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        TableControls.getSelectionModel().addListSelectionListener(new ConfigControlTableSelectionListener());
        ComboBoxDefaultScreen.setModel(new ComboBoxDefaultScreenModel());
       
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        FCMain = new javax.swing.JFileChooser();
        PUControls = new javax.swing.JPopupMenu();
        MIAddControlItem = new javax.swing.JMenuItem();
        MIDeleteControlItem = new javax.swing.JMenuItem();
        DialogControlItem = new javax.swing.JDialog();
        PanelControlItemCenter = new javax.swing.JPanel();
        LabelUserKey = new javax.swing.JLabel();
        TFUserKey = new javax.swing.JTextField();
        LabelVirtualItem = new javax.swing.JLabel();
        ComboBoxVirtualItem = new javax.swing.JComboBox();
        PanelControlItemSouth = new javax.swing.JPanel();
        ButtonControlItemOK = new javax.swing.JButton();
        ButtonControlItemCancel = new javax.swing.JButton();
        PanelSouth = new javax.swing.JPanel();
        ButtonSaveConfig = new javax.swing.JButton();
        ButtonCancel = new javax.swing.JButton();
        TabPaneCenter = new javax.swing.JTabbedPane();
        PanelFileLocations = new javax.swing.JPanel();
        LabelDefaultROMDirectory = new javax.swing.JLabel();
        TFDefaultROMDirectory = new javax.swing.JTextField();
        ButtonBrowseDefaultROMDirectory = new javax.swing.JButton();
        LabelDefaultStateDirectory = new javax.swing.JLabel();
        TFDefaultStateDirectory = new javax.swing.JTextField();
        ButtonBrowseDefaultStateDirectory = new javax.swing.JButton();
        PanelControls = new javax.swing.JPanel();
        SPControls = new javax.swing.JScrollPane();
        TableControls = new javax.swing.JTable();
        ButtonAddControlItem = new javax.swing.JButton();
        ButtonDeleteControlItem = new javax.swing.JButton();
        LabelControlSpacer = new javax.swing.JLabel();
        ButtonDefaultControlItems = new javax.swing.JButton();
        PanelMisc = new javax.swing.JPanel();
        LabelDefaultScreen = new javax.swing.JLabel();
        ComboBoxDefaultScreen = new javax.swing.JComboBox();

        FCMain.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
        MIAddControlItem.setText("Add");
        MIAddControlItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addControlItemActionPerformed(evt);
            }
        });

        PUControls.add(MIAddControlItem);

        MIDeleteControlItem.setText("Delete");
        MIDeleteControlItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteControlItemActionPerformed(evt);
            }
        });

        PUControls.add(MIDeleteControlItem);

        DialogControlItem.setTitle("Add control");
        PanelControlItemCenter.setLayout(new java.awt.GridBagLayout());

        LabelUserKey.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        LabelUserKey.setText("User key :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 40.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        PanelControlItemCenter.add(LabelUserKey, gridBagConstraints);

        TFUserKey.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        TFUserKey.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                TFUserKeyKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                TFUserKeyKeyTyped(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 200.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        PanelControlItemCenter.add(TFUserKey, gridBagConstraints);

        LabelVirtualItem.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        LabelVirtualItem.setText("Virtual action :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 40.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        PanelControlItemCenter.add(LabelVirtualItem, gridBagConstraints);

        ComboBoxVirtualItem.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        PanelControlItemCenter.add(ComboBoxVirtualItem, gridBagConstraints);

        DialogControlItem.getContentPane().add(PanelControlItemCenter, java.awt.BorderLayout.CENTER);

        ButtonControlItemOK.setText("OK");
        ButtonControlItemOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonControlItemOKActionPerformed(evt);
            }
        });

        PanelControlItemSouth.add(ButtonControlItemOK);

        ButtonControlItemCancel.setText("Cancel");
        ButtonControlItemCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonControlItemCancelActionPerformed(evt);
            }
        });

        PanelControlItemSouth.add(ButtonControlItemCancel);

        DialogControlItem.getContentPane().add(PanelControlItemSouth, java.awt.BorderLayout.SOUTH);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("JStella configuration");
        ButtonSaveConfig.setText("Save");
        ButtonSaveConfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonSaveConfigActionPerformed(evt);
            }
        });

        PanelSouth.add(ButtonSaveConfig);

        ButtonCancel.setText("Cancel");
        ButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonCancelActionPerformed(evt);
            }
        });

        PanelSouth.add(ButtonCancel);

        getContentPane().add(PanelSouth, java.awt.BorderLayout.SOUTH);

        PanelFileLocations.setLayout(new java.awt.GridBagLayout());

        LabelDefaultROMDirectory.setText("Default ROM Directory:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        PanelFileLocations.add(LabelDefaultROMDirectory, gridBagConstraints);

        TFDefaultROMDirectory.setText("C:\\");
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 3;
            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
            gridBagConstraints.ipadx = 100;
            gridBagConstraints.weightx = 100.0;
            gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
            PanelFileLocations.add(TFDefaultROMDirectory, gridBagConstraints);

            ButtonBrowseDefaultROMDirectory.setText("Browse");
            ButtonBrowseDefaultROMDirectory.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    ButtonBrowseDefaultROMDirectoryActionPerformed(evt);
                }
            });

            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
            PanelFileLocations.add(ButtonBrowseDefaultROMDirectory, gridBagConstraints);

            LabelDefaultStateDirectory.setText("Default Saved Game Directory:");
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 3;
            gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
            PanelFileLocations.add(LabelDefaultStateDirectory, gridBagConstraints);

            TFDefaultStateDirectory.setText("C:\\");
                gridBagConstraints = new java.awt.GridBagConstraints();
                gridBagConstraints.gridx = 3;
                gridBagConstraints.gridy = 3;
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
                gridBagConstraints.ipadx = 100;
                gridBagConstraints.weightx = 100.0;
                gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
                PanelFileLocations.add(TFDefaultStateDirectory, gridBagConstraints);

                ButtonBrowseDefaultStateDirectory.setText("Browse");
                ButtonBrowseDefaultStateDirectory.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        ButtonBrowseDefaultStateDirectoryActionPerformed(evt);
                    }
                });

                gridBagConstraints = new java.awt.GridBagConstraints();
                gridBagConstraints.gridy = 3;
                gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
                PanelFileLocations.add(ButtonBrowseDefaultStateDirectory, gridBagConstraints);

                TabPaneCenter.addTab("File locations", PanelFileLocations);

                PanelControls.setLayout(new java.awt.GridBagLayout());

                TableControls.setComponentPopupMenu(PUControls);
                TableControls.setModel(new javax.swing.table.DefaultTableModel(
                    new Object [][] {
                        {null, null, null, null},
                        {null, null, null, null},
                        {null, null, null, null},
                        {null, null, null, null}
                    },
                    new String [] {
                        "Title 1", "Title 2", "Title 3", "Title 4"
                    }
                ));
                SPControls.setViewportView(TableControls);

                gridBagConstraints = new java.awt.GridBagConstraints();
                gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
                gridBagConstraints.weightx = 100.0;
                gridBagConstraints.weighty = 100.0;
                PanelControls.add(SPControls, gridBagConstraints);

                ButtonAddControlItem.setText("Add");
                ButtonAddControlItem.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        addControlItemActionPerformed(evt);
                    }
                });

                gridBagConstraints = new java.awt.GridBagConstraints();
                gridBagConstraints.gridx = 2;
                gridBagConstraints.gridy = 2;
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
                PanelControls.add(ButtonAddControlItem, gridBagConstraints);

                ButtonDeleteControlItem.setText("Delete");
                ButtonDeleteControlItem.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        deleteControlItemActionPerformed(evt);
                    }
                });

                gridBagConstraints = new java.awt.GridBagConstraints();
                gridBagConstraints.gridx = 2;
                gridBagConstraints.gridy = 3;
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
                PanelControls.add(ButtonDeleteControlItem, gridBagConstraints);

                LabelControlSpacer.setText(" ");
                gridBagConstraints = new java.awt.GridBagConstraints();
                gridBagConstraints.gridx = 2;
                gridBagConstraints.gridy = 6;
                gridBagConstraints.weighty = 100.0;
                PanelControls.add(LabelControlSpacer, gridBagConstraints);

                ButtonDefaultControlItems.setText("Add default");
                ButtonDefaultControlItems.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        ButtonDefaultControlItemsActionPerformed(evt);
                    }
                });

                gridBagConstraints = new java.awt.GridBagConstraints();
                gridBagConstraints.gridx = 2;
                gridBagConstraints.gridy = 4;
                PanelControls.add(ButtonDefaultControlItems, gridBagConstraints);

                TabPaneCenter.addTab("Controls", PanelControls);

                PanelMisc.setLayout(new java.awt.GridBagLayout());

                LabelDefaultScreen.setText("Default Screen :");
                gridBagConstraints = new java.awt.GridBagConstraints();
                gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
                gridBagConstraints.weightx = 100.0;
                gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
                PanelMisc.add(LabelDefaultScreen, gridBagConstraints);

                ComboBoxDefaultScreen.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
                gridBagConstraints = new java.awt.GridBagConstraints();
                gridBagConstraints.ipadx = 100;
                gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
                gridBagConstraints.weightx = 100.0;
                gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
                PanelMisc.add(ComboBoxDefaultScreen, gridBagConstraints);

                TabPaneCenter.addTab("Misc", PanelMisc);

                getContentPane().add(TabPaneCenter, java.awt.BorderLayout.CENTER);

                pack();
            }// </editor-fold>//GEN-END:initComponents

    private void ButtonDefaultControlItemsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonDefaultControlItemsActionPerformed
       addDefaultControlItems();
    }//GEN-LAST:event_ButtonDefaultControlItemsActionPerformed

    private void TFUserKeyKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TFUserKeyKeyTyped
  evt.consume();
    }//GEN-LAST:event_TFUserKeyKeyTyped

    private void TFUserKeyKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TFUserKeyKeyPressed
        setCurrentVKCode(evt.getKeyCode());
    }//GEN-LAST:event_TFUserKeyKeyPressed

    private void deleteControlItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteControlItemActionPerformed
      deleteControlItem(); 
    }//GEN-LAST:event_deleteControlItemActionPerformed

    private void ButtonControlItemOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonControlItemOKActionPerformed
        myCurrentControlItem=new InputControlBinder(myControlItemKeyCode, myVirtualControlItemArray[ComboBoxVirtualItem.getSelectedIndex()]);
        DialogControlItem.setVisible(false);
    }//GEN-LAST:event_ButtonControlItemOKActionPerformed

    private void ButtonControlItemCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonControlItemCancelActionPerformed
        myCurrentControlItem=null; 
        DialogControlItem.setVisible(false);
    }//GEN-LAST:event_ButtonControlItemCancelActionPerformed

    private void addControlItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addControlItemActionPerformed
        addControlItem();
    }//GEN-LAST:event_addControlItemActionPerformed

    private void ButtonBrowseDefaultStateDirectoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonBrowseDefaultStateDirectoryActionPerformed
        browseForDefaultStateDirectory();
    }//GEN-LAST:event_ButtonBrowseDefaultStateDirectoryActionPerformed

    private void ButtonSaveConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonSaveConfigActionPerformed
         saveConfiguration();
         this.setVisible(false);
    }//GEN-LAST:event_ButtonSaveConfigActionPerformed

    private void ButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonCancelActionPerformed
         this.setVisible(false);
    }//GEN-LAST:event_ButtonCancelActionPerformed

    private void ButtonBrowseDefaultROMDirectoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonBrowseDefaultROMDirectoryActionPerformed
        browseForDefaultROMDirectory();      
        
        
    }//GEN-LAST:event_ButtonBrowseDefaultROMDirectoryActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JStellaConfigurationDialog(new javax.swing.JFrame(), true).setVisible(true);
            }
        });
    }
    
    
    
    private void browseForDefaultROMDirectory()
    {
        File zCurrentDir=new File(TFDefaultROMDirectory.getText());
        if ((zCurrentDir.exists()==true) && (zCurrentDir.isDirectory()==true))
        {
            FCMain.setCurrentDirectory(zCurrentDir);
        }
        else FCMain.setCurrentDirectory(null);
        FCMain.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);      
        int zResult=FCMain.showOpenDialog(this);
        if (zResult==javax.swing.JFileChooser.APPROVE_OPTION)
        {
            TFDefaultROMDirectory.setText(FCMain.getSelectedFile().getAbsolutePath());
        }//end : approved        
    }
    
    private void browseForDefaultStateDirectory()
    {
        File zCurrentDir=new File(TFDefaultStateDirectory.getText());
        if ((zCurrentDir.exists()==true) && (zCurrentDir.isDirectory()==true))
        {
            FCMain.setCurrentDirectory(zCurrentDir);
        }
        else FCMain.setCurrentDirectory(null);
        FCMain.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);      
        int zResult=FCMain.showOpenDialog(this);
        if (zResult==javax.swing.JFileChooser.APPROVE_OPTION)
        {
            TFDefaultStateDirectory.setText(FCMain.getSelectedFile().getAbsolutePath());
        }//end : approved        
    }
    
    
    private void updateControlOptions()
    {
        boolean zDeleteControlEnabled=(TableControls.getSelectedRow()!=-1);
      
        MIDeleteControlItem.setEnabled(zDeleteControlEnabled);
        ButtonDeleteControlItem.setEnabled(zDeleteControlEnabled);
        
        
    }
    
    private void setCurrentVKCode(int aVKCode)
    {
        myControlItemKeyCode=aVKCode;
        TFUserKey.setText("" + KeyEvent.getKeyText(aVKCode));
    }
    
    private void deleteControlItem()
    {
        if (TableControls.getSelectedRow()!=-1)
        {
            myControlItemList.remove(TableControls.getSelectedRow());
            myTableModel.updateTable();
        }//end : row is selected
    }
    
    private void addDefaultControlItems()
    {
       
        for (int i = 0; i < InputMaster.DEFAULT_CONTROL_BINDERS.length; i++) {
            if (myControlItemList.contains(InputMaster.DEFAULT_CONTROL_BINDERS[i])==false) myControlItemList.add(InputMaster.DEFAULT_CONTROL_BINDERS[i]);
            
        }
        myTableModel.updateTable();
    }
    
    
    private void addControlItem()
    {
       //DialogControlItem.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); //J6SE only
        DialogControlItem.setModal(true); //J5SE
       myCurrentControlItem=null;
       setCurrentVKCode(KeyEvent.VK_SPACE);
       ComboBoxVirtualItem.setSelectedIndex(0);
       DialogControlItem.setLocationRelativeTo(this);
       DialogControlItem.setSize(350,175);
       DialogControlItem.setVisible(true);
       
       
       
       if (myCurrentControlItem!=null) myControlItemList.add(myCurrentControlItem);
       myCurrentControlItem=null;
       myTableModel.updateTable();
       
    }
    
    private void createControlItems(java.util.Map<String, String> aCurrentCfg)
    {
         myControlItemList.clear();
         myControlItemList.addAll(InputMaster.convertConfigMapToControlBinderList(aCurrentCfg));
        /* VirtualControlItem[] zVCIArray=VirtualControlItem.values();
         for (int iItem=0; iItem<zVCIArray.length; iItem++)
         {
             if (aCurrentCfg.containsKey(zVCIArray[iItem].getConfigKey())==true)
             {
                 String zValue=aCurrentCfg.get(zVCIArray[iItem].getConfigKey());
                 int[] zValueInts=InputMaster.convertIntegerStringToArray(zValue);
                 for (int iValue=0; iValue<zValueInts.length; iValue++)
                 {
                    myControlItemList.add(new ActualControlItem(zValueInts[iValue], zVCIArray[iItem])); 
                 }//end : for iValue loop
                 
             }//end : has key
             
         }//end : for iItem loop
         */
         
         
    }
    
    private java.util.Map<String, String> showConfigurationDialog(java.util.Map<String, String> aCurrentCfg)
    {
        if (aCurrentCfg!=null)
        {
        if (aCurrentCfg.containsKey(J2600Main.CONFIG_KEY_DEFAULT_ROM_DIR)==true) TFDefaultROMDirectory.setText("" + aCurrentCfg.get(J2600Main.CONFIG_KEY_DEFAULT_ROM_DIR)); 
            else TFDefaultROMDirectory.setText("");
        
        
        if (aCurrentCfg.containsKey(J2600Main.CONFIG_KEY_DEFAULT_STATE_DIR)==true) TFDefaultStateDirectory.setText("" + aCurrentCfg.get(J2600Main.CONFIG_KEY_DEFAULT_STATE_DIR)); 
            else TFDefaultStateDirectory.setText("");
        
        
        
        }//end : cfg not null
        createControlItems(aCurrentCfg);
        
        String zDefaultScreenValue=aCurrentCfg.get(J2600Main.CONFIG_KEY_DEFAULT_SCREEN);
        if (J2600Main.CONFIG_VALUE_DEFAULT_SCREEN_SNOW.equals(zDefaultScreenValue)) ComboBoxDefaultScreen.setSelectedIndex(1);
        else ComboBoxDefaultScreen.setSelectedIndex(0);
       
        
        TableControls.setModel(myTableModel);
        ComboBoxVirtualItem.setModel(new ComboBoxVirtualItemModel());
        updateControlOptions();
        
        
        this.setVisible(true);
        
        return myNewConfiguration;
    }
    
    private String getDefaultScreen()
    {
      int zSelected=ComboBoxDefaultScreen.getSelectedIndex();
      if (zSelected==1) return J2600Main.CONFIG_VALUE_DEFAULT_SCREEN_SNOW;
      else return J2600Main.CONFIG_VALUE_DEFAULT_SCREEN_TEST_PATTERN;
    }
    
    private void saveConfiguration()
    {
       // StringBuffer zSB=new StringBuffer();
        myNewConfiguration=new java.util.HashMap<String, String>();
        myNewConfiguration.put(J2600Main.CONFIG_KEY_DEFAULT_ROM_DIR, TFDefaultROMDirectory.getText());
         myNewConfiguration.put(J2600Main.CONFIG_KEY_DEFAULT_STATE_DIR, TFDefaultStateDirectory.getText());
         myNewConfiguration.put(J2600Main.CONFIG_KEY_DEFAULT_SCREEN, getDefaultScreen());
         InputMaster.addControlBinderListToConfigMap(myControlItemList, myNewConfiguration);
        
    }
    
    public static java.util.Map<String, String> runJStellaConfiguration(java.awt.Frame aParent, java.util.Map<String, String> aCurrentConfig)
    {
       JStellaConfigurationDialog zConfigDlg=new JStellaConfigurationDialog(aParent, true);
       zConfigDlg.setLocationRelativeTo(aParent);
       java.util.Map<String,String> zReturn=zConfigDlg.showConfigurationDialog(aCurrentConfig);
       
       return zReturn;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonAddControlItem;
    private javax.swing.JButton ButtonBrowseDefaultROMDirectory;
    private javax.swing.JButton ButtonBrowseDefaultStateDirectory;
    private javax.swing.JButton ButtonCancel;
    private javax.swing.JButton ButtonControlItemCancel;
    private javax.swing.JButton ButtonControlItemOK;
    private javax.swing.JButton ButtonDefaultControlItems;
    private javax.swing.JButton ButtonDeleteControlItem;
    private javax.swing.JButton ButtonSaveConfig;
    private javax.swing.JComboBox ComboBoxDefaultScreen;
    private javax.swing.JComboBox ComboBoxVirtualItem;
    private javax.swing.JDialog DialogControlItem;
    private javax.swing.JFileChooser FCMain;
    private javax.swing.JLabel LabelControlSpacer;
    private javax.swing.JLabel LabelDefaultROMDirectory;
    private javax.swing.JLabel LabelDefaultScreen;
    private javax.swing.JLabel LabelDefaultStateDirectory;
    private javax.swing.JLabel LabelUserKey;
    private javax.swing.JLabel LabelVirtualItem;
    private javax.swing.JMenuItem MIAddControlItem;
    private javax.swing.JMenuItem MIDeleteControlItem;
    private javax.swing.JPopupMenu PUControls;
    private javax.swing.JPanel PanelControlItemCenter;
    private javax.swing.JPanel PanelControlItemSouth;
    private javax.swing.JPanel PanelControls;
    private javax.swing.JPanel PanelFileLocations;
    private javax.swing.JPanel PanelMisc;
    private javax.swing.JPanel PanelSouth;
    private javax.swing.JScrollPane SPControls;
    private javax.swing.JTextField TFDefaultROMDirectory;
    private javax.swing.JTextField TFDefaultStateDirectory;
    private javax.swing.JTextField TFUserKey;
    private javax.swing.JTabbedPane TabPaneCenter;
    private javax.swing.JTable TableControls;
    // End of variables declaration//GEN-END:variables
    
    
    private class ConfigControlTableModel extends javax.swing.table.AbstractTableModel 
    {
        
        
        public Object getValueAt(int rowIndex, int columnIndex) {
            if (columnIndex==0) return myControlItemList.get(rowIndex).getActualKeyText();
            else return myControlItemList.get(rowIndex).getTarget().toString();
        }

        public int getRowCount() {
            return myControlItemList.size();
        }

        public int getColumnCount() {
            return 2;
        }
        
        public void updateTable()
        {
            fireTableStructureChanged();
        }

        public String getColumnName(int column) {
          if (column==0) return "User key";
          else return "Virtual control action";
        }
        
        
        
    }
    
    //=====================================================
    
   private class ConfigControlTableSelectionListener implements ListSelectionListener
   {
        public void valueChanged(ListSelectionEvent e) {
            updateControlOptions();
        }
       
   }
    
    
    //========================================================
    
    private class ComboBoxVirtualItemModel extends javax.swing.DefaultComboBoxModel 
    {
        public int getSize() {
            return myVirtualControlItemArray.length;
        }

        public Object getElementAt(int index) {
            return myVirtualControlItemArray[index];
        }
        
    }
    
    
    
      private class ComboBoxDefaultScreenModel extends javax.swing.DefaultComboBoxModel 
    {
        public int getSize() {
            return 2;
        }

        public Object getElementAt(int index) {
            if (index==0) return "Color Test Pattern";
            else return "Television Snow";
        }
        
    }
    
    
    
}
